package fr.umlv.m2.uwp2011.service.user.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import fr.umlv.m2.uwp2011.persistence.user.User_Cursus;
import fr.umlv.m2.uwp2011.persistence.user.User_Matiere;
import fr.umlv.m2.uwp2011.persistence.user.dao.HibernateUser_CursusDAO;
import fr.umlv.m2.uwp2011.persistence.user.dao.HibernateUser_MatiereDAO;
import fr.umlv.m2.uwp2011.service.user.BouchonUserDAO;
import fr.umlv.m2.uwp2011.service.user.IUserService;
import fr.umlv.m2.uwp2011.service.user.UserDTO;

@Service("userService")
public class UserServiceImpl implements IUserService {

	private final BouchonUserDAO userDAO  = new BouchonUserDAO();
	private final HibernateUser_MatiereDAO user_matiereDAO = new HibernateUser_MatiereDAO();
	private final HibernateUser_CursusDAO user_cursusDAO = new HibernateUser_CursusDAO();

	@Override
	public UserDTO getUser(String login) {
		UserDTO user = userDAO.getUserDetails(login);
		return user;
	}
	
	@Override
	public UserDTO getUser(String login, String password) {
		UserDTO user = userDAO.getUserDetails(login, password);
		return user;
	}
	
	@Override
	public List<UserDTO> getUserByCursusID(Long cursus_id) {
		List<User_Cursus> listUserCursus = user_cursusDAO.findUser_CursusByCursusID(cursus_id);
		List<UserDTO> users = new ArrayList<UserDTO>(listUserCursus.size());
		for(User_Cursus userCusus : listUserCursus) {
			UserDTO userDTO = userDAO.getUserDetails(userCusus.getUser_id());
			users.add(userDTO);
		}
		return users;
	}

	@Override
	public List<UserDTO> getUserByMatiereID(Long matiere_id) {
		List<User_Matiere> listUserMatiere = user_matiereDAO.findUser_MatiereByMatiereID(matiere_id);
		List<UserDTO> users = new ArrayList<UserDTO>(listUserMatiere.size());
		for(User_Matiere userMatiere : listUserMatiere) {
			UserDTO userDTO = userDAO.getUserDetails(userMatiere.getUser_id());
			users.add(userDTO);
		}
		return users;
	}

	@Override
	public List<UserDTO> getUserByCususAndMatiere(Long cursus_id, Long matiere_id) {
		List<UserDTO> users = getUserByCursusID(cursus_id);
		users.retainAll(getUserByMatiereID(matiere_id));
		return users;
	}

}
